// JavaScript Document
//registeration
function register(currentPath)
{
		var firstname		= $('firstname').value;
		var lastname		= $('lastname').value;

		$('ajax_loader').style.display	= 'block';
		$('registerResult').innerHTML	= '';
		
		var email		=$('register_email').value;
		var password	=$('register_password').value;

		var url		= currentPath+'ajax';
		var pars	= 'action=register&firstname='+firstname+'&lastname='+lastname+'&email='+email+'&pass='+password;
		//alert(pars);
		var myAjax	= new Ajax.Request(
			url, 
			{
				parameters: pars, 
				onSuccess: updateRegister
			},email);
}

function updateRegister(originalRequest,json,email)
{
	var responseText = originalRequest.responseText;
	if(responseText == 'success')
		{
			$('registration').style.display		= 'none';
			$('registerSuccess').style.display	= 'block';
			$('registerEmail').innerHTML		= email;
	
		}
	else
		$('registerResult').innerHTML	= '<div class="msg-error">'+responseText+'</div>';
	$('ajax_loader').style.display	= 'none';
}

//---------------------------------------------------------------------------------------------------------------------
//edit basic profile
function generalProfile(currentPath,userId)
{
		$('basic_ajax_loader').style.display	= 'block';
		var sex			= $('sex').value;
		//alert(meeting);
		var relation	= $('relationship').value;
		var lookingFor	= '';
		var interestedIn	= '';
		for(var i=1;i<=$('totMeets').value;i++)
			if($('meeting_for'+i).checked)
				lookingFor +=$('meeting_for'+i).value+',';
		for(var i=1;i<=$('totMeets2').value;i++)
			if($('meeting'+i).checked)
				interestedIn +=$('meeting'+i).value+',';
				
		//alert(lookingFor);
		//alert(interestedIn);
		
		var bd_day		= $('birthday_day').value;
		var bd_month	= $('birthday_month').value;
		var bd_year		= $('birthday_year').value;
		var bd_visible	= $('birthday_visibility').value;
		var city		= $('city_select').value;
		var state	= '';
		var politics	= $('political_view').value;
		var religion	= $('religion_name').value;
		
		var url		= currentPath+'ajax';
		var pars	= 'action=generalProfile&userId='+userId+'&sex='+sex+'&interestedIn='+interestedIn+'&relation='+relation+'&lookingFor='+lookingFor+'&bd='+bd_day+'&bm='+bd_month+'&by='+bd_year+'&bd_visible='+bd_visible+'&city='+city+'&politics='+politics+'&religion='+religion+'&state='+state;
		//alert(pars);
		var myAjax	= new Ajax.Request(
			url, 
			{
				parameters: pars, 
				onSuccess: updateGeneralProfile
			});
}

function updateGeneralProfile(originalRequest)
{
	var responseText				= originalRequest.responseText;
	//alert(responseText);
	$('profileResult').innerHTML	= '<div class="msg-good">'+responseText+'</div>';
	$('basic_ajax_loader').style.display	= 'none';
}

function personalProfile(currentPath,userId)
{
		$('personal_ajax_loader').style.display	= 'block';
		
		var activities	= $('clubs').value;
		var interests	= $('interests').value;
		var music		= $('music').value;
		var tv			= $('tv').value;
		var movies		= $('movies').value;
		var books		= $('books').value;
		var quote		= $('quote').value;
		var about_me	= $('about_me').value;
				
		var url		= currentPath+'ajax';
		var pars	= 'action=submit_personal&userId='+userId+'&activities='+encodeURIComponent(activities)+'&interests='+encodeURIComponent(interests)+'&music='+encodeURIComponent(music)+'&tv='+encodeURIComponent(tv)+'&movies='+encodeURIComponent(movies)+'&books='+encodeURIComponent(books)+'&quote='+encodeURIComponent(quote)+'&about_me='+encodeURIComponent(about_me);
		//alert(pars);
		var myAjax	= new Ajax.Request(
			url, 
			{
				parameters: pars, 
				onSuccess: updatePersonalProfile
			});
}

function updatePersonalProfile(originalRequest)
{
	var responseText				= originalRequest.responseText;
	//alert(responseText);
	$('personalResult').innerHTML	= '<div class="msg-good">'+responseText+'</div>';
	$('personal_ajax_loader').style.display	= 'none';
}


function contactProfile(currentPath,userId)
{
		$('contact_ajax_loader').style.display	= 'block';
		
		var mobile		= $('mobile').value;
		var land_line	= $('other_phone').value;
		var address		= $('address').value;
		var website		= $('website').value;
		var icq			= $('icq').value;
		var skype		= $('skype').value;
				
		var url		= currentPath+'ajax';
		var pars	= 'action=submit_contact&userId='+userId+'&mobile='+mobile+'&land_line='+land_line+'&address='+address+'&website='+website+'&icq='+icq+'&skype='+skype;
		//alert(pars);
		var myAjax	= new Ajax.Request(
			url, 
			{
				parameters: pars, 
				onSuccess: updateContactProfile
			});
}

function updateContactProfile(originalRequest)
{
	var responseText				= originalRequest.responseText;
	//alert(responseText);
	$('contact_result').innerHTML	= '<div class="msg-good">'+responseText+'</div>';
	$('contact_ajax_loader').style.display	= 'none';
}


function messageProcess(folder,currentPath)
{
	var flag	= false;//defualt flag for check box
	var msgIds	= '';//declare msgIds to store selected message ids
	var action	= $('message_action').value;
	var allNodes	= document.getElementById('msgForm');
	//alert(action);
	//alert(folder);
	var url		= currentPath+'ajax';
	var pars	= 'action=message_'+action;
	if(action=='mark_unread' || action=='mark_read' || action=='move_to_trash' || action=='select_action')
	{
		if(action=='mark_unread' && folder=='inbox')
			var msgName	= 'mess_i_r_';
		else if(action=='mark_unread' && folder=='sent')
			var msgName = 'mess_s_r_';
		else if(action=='mark_read' && folder=='inbox')
			var msgName	= 'mess_i_u_';
		else if(action=='mark_read' && folder=='sent')
			var msgName = 'mess_s_u_';
		else if(action=='select_action' && folder=='inbox')
			var msgName	= 'mess_i_r_';
		else if(action=='select_action' && folder=='sent')
			var msgName	= 'mess_s_r_';

			
			var len = $('msgCnt').value;
			for(i = 0; i < allNodes.length; i++) 
			{
				//do something to each form field
				//alert(allNodes[i].name);
				for (j = 1; j <= len; j++)
				{
					var msg1='mess_i_r_' + j;
					var msg2='mess_i_u_' + j;
					var msg3='mess_s_r_' + j;
					var msg4='mess_s_u_' + j;
					if(allNodes[i].name	==msg1)
						msgName=msg1;
					else if(allNodes[i].name==msg2)
						msgName=msg2;
					else if(allNodes[i].name==msg3)
						msgName=msg3;
					else if(allNodes[i].name==msg4)
						msgName=msg4;
					else
						msgName='';
					if(msgName!='' && $(msgName).checked)
						{
							flag = true;
							msgIds += $(msgName).value+',';
						}
				}
			}
		
		pars  +='&folder='+folder+'&msgIds='+msgIds;
	}
	//alert(flag);
	if((action=='mark_unread' || action=='mark_read') && !flag)
		alert("Жодного повідомлення не виділено");
	else
	{
			var myAjax	= new Ajax.Request(
				url, 
				{
					parameters: pars, 
					onSuccess: updateMessageProcess
				});
	}
}

function msgSingleDelete(delId,currentPath,action)
{
	//alert(delId);
	var ans	= confirm("Ви дійсно бажаєте видалити це повідомлення?");
	//alert(ans);
	if(ans)
	{
		var url		= currentPath+'ajax';
		if(action=='trash')
			var pars	= 'action=msgTrashSingleDel&delId='+delId;
		else
			var pars	= 'action=msgSingleDel&delId='+delId;
		//alert(pars)	
		var myAjax	= new Ajax.Request(
			url, 
			{
				parameters: pars, 
				onSuccess: updateMessageProcess
			});
	}
}
function updateMessageProcess(originalRequest)
{
	var responseText			= originalRequest.responseText;
		//alert(responseText);
	//alert(responseText);
	$('message_content_div').innerHTML = responseText;
}

function wallMsgDelete(delId, profileId, userId, currentPath)
{
	//alert(delId);
	var ans	= confirm("Ви дійсно бажаєте видалити це повідомлення?");
	//alert(ans);
	if(ans)
	{
		var url		= currentPath+'ajax';
		var pars	= 'action=wallMsgDel&delId='+delId+'&profileId='+profileId+'&userId='+userId;
		$('wall_post_ajax_loader').style.display	= 'block';
		//alert(pars)	
		var myAjax	= new Ajax.Request(
			url, 
			{
				parameters: pars, 
				onSuccess: updateWallMessageProcess
			});
	}
}
function updateWallMessageProcess(originalRequest)
{
	var responseText			= originalRequest.responseText;
	$('wall_post_ajax_loader').style.display	= 'none';
	$('wall_post_result').innerHTML = '<div class="msg-good">Запис успішно видалено</div>'+responseText;
}

function selectAllMsg()
{
	allNodes	= document.getElementById('msgForm');
	msgIds	 	= '';
	var flag	= false;//defualt flag for check box
	var len = $('msgCnt').value;
	for(i = 0; i < allNodes.length; i++) 
	{
		//do something to each form field
		//alert(allNodes[i].name);
		for (j = 1; j <= len; j++)
		{
			var msg1='mess_i_r_' + j;
			var msg2='mess_i_u_' + j;
			var msg3='mess_s_r_' + j;
			var msg4='mess_s_u_' + j;
			if(allNodes[i].name	==msg1)
				msgName=msg1;
			else if(allNodes[i].name	==msg2)
				msgName=msg2;
			else if(allNodes[i].name	==msg3)
				msgName=msg3;
			else if(allNodes[i].name	==msg4)
				msgName=msg4;
			else
				msgName='';
			if(msgName!='' && $(msgName).checked)
				{
					flag = true;
					msgIds += $(msgName).value+',';
				}
		}
	}
	return msgIds;
}
function trashDelete(currentPath)
{
	var url		= currentPath+'ajax';
	var pars	= 'action=trash_delete';
	allNodes 	= $A($('msgForm'));
	inboxMsgIds	= '';
	sentMsgIds	= '';
	var len = $('msgCnt').value;
	
	for(i = 0; i < allNodes.length; i++) 
	{
		//do something to each form field
		for (j = 1; j <= len; j++)
		{
			var msg1='mess_i_' + j;
			var msg2='mess_s_' + j;
			if(allNodes[i].name	== msg1)
			{
				if($(msg1).checked)
				{
					flag = true;
					inboxMsgIds += $(msg1).value+',';
				}
			}
			if(allNodes[i].name	== msg2)
			{
				if($(msg2).checked)
				{
					flag = true;
					sentMsgIds += $(msg2).value+',';
				}
			}
		}//end second for
	}//end first for
	
	pars +='&inboxMsgIds='+inboxMsgIds+'&sentMsgIds='+sentMsgIds;
	if(!flag)
		alert("Жодного повідомлення не виділено");
	else
	{
		var myAjax	= new Ajax.Request(
			url, 
			{
				parameters: pars, 
				onSuccess: updateMessageProcess
			});
	}
}//end function

//--------------------------------------------------------------------------------------------------
//Remove school in education profile----------------------------------------------------------------
function remove_school(school_name,currentPath) 
{
		var ans	= confirm("Ви дійсно бажаєте видалити цю школу з бази данних?");
		if(ans)
		{
			var strURL	= currentPath+'ajax';
			
			var cons	= Array();
			//retrieve school name
			var	school	= $(school_name+'_school_name').value;
			//retrieve school year
			if(parseInt($(school_name+'_year').value)>0)
				var school_year	= $(school_name+'_year').value
			else
				var school_year	= '';
			//retrieve constration value
			for(var i=1;i<=parseInt($(school_name+'_concentration_value_count').value);i++)
				{
					cons[i-1]	= $(school_name+'_concentration'+i+'_name').value;
				}
			//retrieve attended for
		
			var pars	= 'action=remove_school&cons='+cons+'&school='+school+'&school_year='+school_year+'&education='+school_name;
			//alert(pars);
			//var pars	= 'action=remove_school&education='+school_name;
			var arg		= school_name+'|'+currentPath;
			//alert(pars);
			var myAjax	= new Ajax.Request(
				strURL, 
				{
					parameters: pars, 
					onSuccess: updateRemove_school
				},arg);
		}
}
function updateRemove_school(originalRequest,json,arg)
{
	var responseText	= originalRequest.responseText;
	var argsplit	= arg.split("|");
	
	$('education_result').style.display	= 'block';
	if(argsplit[0]!='education_1' && responseText=='true')
		{
			$(argsplit[0]).style.display	= 'block';
			result	= '<div class="msg-good">Зміни успішно збережено<div>';
		}
	else if(responseText=='true')
		result	= '<div class="msg-good">Навчальний заклад успішно видалено</div>';
	else
		result	= '<div class="msg-error">Щось пішло не так! Навчальний заклад невдалося видалити</div>';
	$('education_result').innerHTML	=	result;						
	var t= setTimeout("showRemoveSchoolResult(\""+argsplit[1]+"\")",2000);
}
	

function showRemoveSchoolResult(curPath)
{
	//alert(curPath);
	window.location	= curPath+"edit/education";
}
//--------------------------------------------------------------------------------------------------
//Remove school in education profile----------------------------------------------------------------
function remove_job(job_name,currentPath) 
{
		var ans	= confirm("Ви дійсно бажаєте видалити це місце роботи з бази данних?");
		if(ans)
		{
			var strURL	= currentPath+'ajax';
			
			var pars	= 'action=remove_job&job='+job_name;
			//alert(pars);
			var arg		= job_name+'|'+currentPath;
			var myAjax	= new Ajax.Request(
				strURL, 
				{
					parameters: pars, 
					onSuccess: updateRemove_job
				},arg);
		}
}
function updateRemove_job(originalRequest,json,arg)
{
	var responseText	= originalRequest.responseText;
	var argsplit		= arg.split("|");
	
		$('work_history_result').style.display	= 'block';
	if(argsplit[0]!='work_history_1' && responseText=='true')
		{
			$(argsplit[0]).style.display	= 'block';
			result	= '<div class="msg-good">Зміни успішно збережено<div>';
		}
	else if(responseText=='true')
		result	= '<div class="msg-good">Місце роботи успішно видалено<div>';
	else
		result	= '<div class="msg-error">Щось пішло не так! Місце роботи невдалося видалити</div>';
	$('work_history_result').innerHTML	=	result;						
	var t= setTimeout("showRemoveWorkResult(\""+argsplit[1]+"\")",2000);
}
//--------------------------------------------------------------------------------------------------
function showRemoveWorkResult(curPath)
{
	//alert(curPath);
	window.location	= curPath+"edit/work";
}

//add friend to current users friends list
function addfriend(currentPath,friendId)
{
	var strURL				= currentPath+'ajax';
	var pars		= 'action=add_friend&friendId='+friendId;
	//alert(pars);
	$('ajax_loader').style.display	= 'block';
	var myAjax	= new Ajax.Request(
		strURL, 
		{
			parameters: pars, 
			onSuccess: updateAddfriend
		});
	$('add_friend_error').style.display		= 'none';
}

function updateAddfriend(originalRequest)
{
	var responseText	= originalRequest.responseText;
	//alert(responseText);
	if(responseText=='success')
		{
			$('add_friend_success').style.display	= 'block';
			$('form-submit').style.display			= 'none';
		}
	else
		{
			$('add_friend_success').style.display	= 'none';
			$('add_friend_section').style.display	= 'block';
			$('add_friend_error').style.display		= 'block';
			$('add_friend_error').innerHTML			= responseText;
		}
	$('ajax_loader').style.display	= 'none';
}

function sendMessage(actionId,actionType,currentPath)
{
	var url		= currentPath+'ajax';
	
	var toname	= $('mailTo').value;
	var subject	= $('mailSubject').value;
	var content	= $('mailContent').value;
	
	var pars	='action=sendMessage&subject='+subject+'&content='+encodeURIComponent(content)+'&toname='+toname;
	
	//alert(pars);
	$('ajax_loader').style.display	= 'block';
	var myAjax	= new Ajax.Request(
		url, 
		{
			parameters: pars, 
			onSuccess: updateSendMessage
		});
}
function updateSendMessage(originalRequest)
{
	var responseText				= originalRequest.responseText;
	//alert(responseText);
	var splitRes					= responseText.split("|");
	if(splitRes[1]=='yes')
		{
			$('send_message_div').style.display	= 'none';
		}
	$('send_result').innerHTML		= splitRes[0];
	//$('send_message_div').style.display	= 'none';
	$('ajax_loader').style.display	= 'none';
	
}

//process the rsvp status changes for event main page
function changeUserStatus(userId,currentPath,dowhat)
{
	var url		= currentPath+'ajax';
	if(dowhat=='store')
		var status	= $('screenStatusSelect').value;
	var pars	= 'action=changeUserStatus&uid='+userId+'&status='+encodeURIComponent(status)+'&dowhat='+dowhat;
	//alert(pars);
	$('status_ajax_loader').style.display	= 'block';
	var myAjax	= new Ajax.Request(
		url, 
		{
			parameters: pars, 
			onSuccess: updateChangeUserStatus
		});
}
function updateChangeUserStatus(originalRequest)
{
	var responseText							= originalRequest.responseText;
	//alert(responseText);
	$('screen_status_div').style.display		= 'block';
	$('screen_status_div').innerHTML			= responseText;
	$('status_ajax_loader').style.display		= 'none';
	$('screen_status_change_div').style.display	= 'none';
}

//post wall for user in profile
function post2Profile(postedBy,postedTo,currentPath)
{
	var url		= currentPath+'ajax';
	var postedValue	= $('post_content').value;

	$('wall_post_ajax_loader').style.display	= 'block';
	
	if (postedValue == '') {
	$('wall_post_ajax_loader').style.display	= 'none';
	result	= '<div class=\"msg-error\">Помилка: Неможливо відправити пусте повідомлення</div>';
	$('wall_post_result2').innerHTML	=	result;							
	} else {
	
	var pars	= 'action=postToProfile&by='+postedBy+'&to='+postedTo+'&content='+encodeURIComponent(postedValue);

	var myAjax	= new Ajax.Request(
		url, 
		{
			parameters: pars, 
			onSuccess: updatePost2Profile
		},currentPath);
	}
}
function updatePost2Profile(originalRequest)
{
	var responseText						= originalRequest.responseText;
	$('wall_post_ajax_loader').style.display= 'none';
	$('wall_post_result').innerHTML			= '<div class="msg-good">Запис успішно опублікований</div>'+responseText;
}

function post2Wall(postedBy,postedTo,currentPath)
{
	var url		= currentPath+'ajax';
	var postedValue	= $('post_content').value;

	$('wall_post_ajax_loader').style.display	= 'block';
	
	if (postedValue == '') {
	$('wall_post_ajax_loader').style.display	= 'none';
	result	= '<div class=\"msg-error\">Помилка: Неможливо відправити пусте повідомлення!!</div>';
	$('wall_post_result2').innerHTML	=	result;							
	} else {
	
	var pars	= 'action=postToWall&by='+postedBy+'&to='+postedTo+'&content='+encodeURIComponent(postedValue);

	var myAjax	= new Ajax.Request(
		url, 
		{
			parameters: pars, 
			onSuccess: updatePost2Wall
		},currentPath);
	

	
	}
}
function updatePost2Wall(originalRequest)
{
	var responseText						= originalRequest.responseText;
	$('wall_post_ajax_loader').style.display= 'none';
	$('wall_post_result').innerHTML			= '<div class="msg-good">Запис успішно опублікований</div>';
	window.location	= responseText;
}